package com.diqizu.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.diqizu.dao.ImgDaoImpl;
import com.diqizu.entity.Images;
import com.diqizu.servlet.BaseServlet;
import com.diqizu.utli.Result;

/**
 * Servlet implementation class chanpinimgServletadmin
 */
@WebServlet("/admin/ChanPinImgServletadmin")
public class ChanPinImgServletadmin extends BaseServlet {
	ImgDaoImpl dao=new ImgDaoImpl();
	public void getimglist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		ArrayList<Images> imgall = dao.imgall();
       String res = Result.toClient(0, "产品图片管理", imgall);
		PrintWriter out = resp.getWriter();
		out.write(res);
		out.flush();
		out.close();
		
	}
	
	public void insertimg(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		//获取数据
		String imgurl = req.getParameter("imgurl");
		String productid = req.getParameter("productid");
		String imgname = req.getParameter("imgname");
		System.out.println(imgurl+productid+imgname);
		Images img=new Images(0,imgurl,Integer.valueOf(productid),imgname);
		
		 boolean res=dao.insertproduct(img);
		 
		 PrintWriter out = resp.getWriter();
		 if(res) {
			 out.write(Result.toClient(0, "添加成功"));
		 }else {
			 out.write(Result.toClient(1, "添加失败"));
		 }
			out.flush();
			out.close();
			
		}
	
	public void updateproduct(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//获取数据
		String imgid=req.getParameter("imgid");
		String imgurl = req.getParameter("imgurl");
		String productid = req.getParameter("productid");
		String imgname = req.getParameter("imgname");
		
		Images img=new Images(Integer.valueOf(imgid),imgurl,Integer.valueOf(productid),imgname);
		 boolean res = dao.updateproductimg(img);
		 PrintWriter out = resp.getWriter();
		 if(res) {
			 out.write(Result.toClient(0, "修改成功"));
		 }else {
			 out.write(Result.toClient(1, "修改失败"));
		 }
			out.flush();
			out.close();
		}
	public void deleteproductimg(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		//获取数据
		
		String imgid = req.getParameter("imgid");
		int proid2=Integer.valueOf(imgid);
		
		
		 boolean res = dao.deleteproductimg(proid2);
		 
		 PrintWriter out = resp.getWriter();
		 if(res) {
			 out.write(Result.toClient(0, "删除成功"));
		 }else {
			 out.write(Result.toClient(1, "删除失败"));
		 }
			out.flush();
			out.close();
			
		}
	public void getProImgList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1.获取页面传来的参数
		String pid = req.getParameter("pid");
	
		// 2.调用dao层的方法获取数据，当前页
		ArrayList<Images> list = dao.getListByPid(pid);
		
		// 将数据封装json
		String res = Result.toClient(0, "产品列表",list);

		// 3.将数据返回到页面
		PrintWriter out = resp.getWriter();
		out.write(res);
		out.flush();
		out.close();
	}

}
